<template>
  <div>
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @cancel="$emit('close')" @confirm="onConfirm" />
  </div>
</template>

<script>
import dayJs from 'dayjs'
import { updateUser } from '@/api/user'
export default {
  name: 'UpdateBirthday',
  props: {
    birthday: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.birthday)
    }
  },
  methods: {
    async onConfirm (value) {
      var birthdayTime = dayJs(this.currentDate).format('YYYY-MM-DD')
      try {
        const { data: res } = await updateUser({
          birthday: birthdayTime
        })
        console.log(res)
        this.$emit('close')
        this.$emit('update', birthdayTime)
        this.$toast.success('更新生日成功')
      } catch (err) {
        this.$toast.fail('更新生日失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
